package com.mutual.appUserInfo.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.mutual.app.pojo.AppResult;
import com.mutual.appUserInfo.dao.AppUserInfoDao;
import com.mutual.appUserInfo.dao.AppUserInfoProductDao;
import com.mutual.appUserInfo.pojo.AppUserInfo;
import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.util.SystemUtil;

@Service
public class AppUserInfoServiceImpl implements AppUserInfoService {

	@Resource
	private AppUserInfoDao appUserInfoDao;
	
	@Resource
	private AppUserInfoProductDao appUserInfoProductDao;
	
	@Override
	public void insert(AppUserInfo entity) {
		appUserInfoDao.insertSelective(entity);
	}

	@Override
	public void updateByPrimaryKey(AppUserInfo entity) {
		entity.setUpdateDate(new Date());
		appUserInfoDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public AppUserInfo selectByPrimaryKey(Serializable entityid) {
		return appUserInfoDao.selectByPrimaryKey(entityid);
	}

	@Override
	public List<AppUserInfo> selectAppUserInfoList(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return appUserInfoDao.selectObjectList(param);
	}

	@Override
	public int selectAppUserInfoCount(Map<String, Object> param) {
		return appUserInfoDao.selectTotalCount(param);
	}

	@Override
	public void updateAppUserTypeBatch(Map<String, Object> param) {
		appUserInfoDao.updateAppUserTypeBatch(param);
	}

	@Override
	public List<AppUserInfoProduct> selectAppUserInfoProduct(AppUserInfoProduct t) {
		return appUserInfoProductDao.selectObjectList(t);
	}

	@Override
	public AppResult selectPensonalStatistics(int appUserId) {
		AppResult appResult=new AppResult();
		appResult.setData(appUserInfoDao.selectPensonalStatistics(appUserId));
		appResult.setStatus(AppResult.STATUS_SUCCESS);
		appResult.setMsg(AppResult.MSG_SELECT_SUCCESS);
		return appResult;
	}

	@Override
	public AppUserInfo appUserLogin(AppUserInfo appUserInfo) {
		appUserInfo.setPassword(DigestUtils.md5DigestAsHex(appUserInfo.getPassword().getBytes()));
		return appUserInfoDao.appUserLogin(appUserInfo);
	}

	@Override
	public AppUserInfo regist(AppUserInfo appUserInfo) {
		int maxId=appUserInfoDao.selectMaxId()+1;
		appUserInfo.setLoginName("互助用户"+maxId);
		Date now=new Date();
		appUserInfo.setCreateDate(now);
		appUserInfo.setUpdateDate(now);
		appUserInfo.setPassword(DigestUtils.md5DigestAsHex(appUserInfo.getPassword().getBytes()));
		appUserInfoDao.insertSelective(appUserInfo);
		return appUserInfo;
	}

	@Override
	public void resetPassword(AppUserInfo appUserInfo) {
		appUserInfo.setPassword(DigestUtils.md5DigestAsHex(appUserInfo.getPassword().getBytes()));
		appUserInfoDao.resetPassword(appUserInfo);
	}

	@Override
	public AppResult getSMSCode(String mobile) {
		return null;
	}
}
